package com.sky.controller.admin;

import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * ClassName: CommonController
 * Package: com.sky.controller.admin
 * Description:
 *  通用接口
 * @Author: qmh
 * @Create: 2024/12/23 - 21:33
 * @Version: v1.0
 */
@RestController
@RequestMapping("/admin/common")
@Api(tags = "通用接口")
@Slf4j
public class CommonController {
    @Value("${image.save.path}")
    private String SAVE_PATH;

    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public Result<String> upload(MultipartFile file)  {
        log.info("文件上传 {}",file);
        String nameAndType = file.getOriginalFilename(); // XXX.jpg png
        log.info("上传的文件名为：{}",nameAndType);
        String path = SAVE_PATH + "/" + nameAndType;

        File dest = new File(path);
        if(!dest.exists()){
            dest.mkdir();
        }
        try {
            file.transferTo(dest);
            return Result.success(path); // 文件访问路径
        } catch (IOException e) {
            log.error("文件上传失败：{}",e.getMessage());
        }

        return Result.error("文件上传失败");
    }
}